﻿#coding:utf-8

from Primitives import *
from pygame.locals import *

class Button(GameObject):
	
	def __init__(self,id,onClick):		
		self._has_updated = False		
		
		self.id = id
		self.onClick = onClick
		
		super(Button,self).__init__(id,ButtonEventManager())
		
		self.backcolor = (150,150,150)
		self.label = "Este e um botao grande"		
		
		self.rect = pygame.Rect( (100,100), (50,20) )
		self.image = pygame.Surface( (self.rect.w,self.rect.h) )
		self.image = self.image.convert()
		self.image.fill( self.backcolor )	
		
		self.font = pygame.font.Font(None,22)
	
		
	def pack(self):
		self.rect.w = self.font.size(self.label)[0] + 10
		self.rect.h = self.font.size(self.label)[1] + 10				
		
		self._has_updated = True
		
	def update(self):
		if self._has_updated:
						
			self.image = pygame.Surface( (self.rect.w,self.rect.h) )
			self.image = self.image.convert()
			self.image.fill( self.backcolor )			
									
			labelRect = pygame.Rect( (2,2), self.font.size(self.label) )
			labelRect.centerx = self.image.get_rect().centerx	
			labelRect.centery = self.image.get_rect().centery	
			labelSurf = self.font.render( self.label, 1, (0,0,0) )	

			self.image.blit( labelSurf,labelRect )	

			self._has_updated = False
			
		
class ButtonEventManager(EventManager):
	def __init__(self):
		self._has_clicked = False
		
	def fireEvent(self,scene,go,event):
		if event.type == MOUSEBUTTONDOWN:
			if go.rect.collidepoint(event.pos):
				self._has_clicked = True
			
		elif event.type == MOUSEBUTTONUP:
			if go.rect.collidepoint(event.pos) and self._has_clicked == True:
				go.onClick(go)
				self._has_clicked=False

				
class Label(GameObject):

	def __init__(self,id):		
		self._has_updated = False		
		
		self.id = id
		super(Label,self).__init__(id,EventManager())
		
		self.backcolor = (255,255,255)
		self.label = "Label: "		
		
		self.rect = pygame.Rect( (100,100), (50,20) )
		self.image = pygame.Surface( (self.rect.w,self.rect.h) )
		self.image = self.image.convert()
		self.image.fill( self.backcolor )	
		
		self.font = pygame.font.Font(None,16)
		
		self.max_w = None
	
		
	def pack(self):
		self.rect.w = self.font.size(self.label)[0] + 10
		self.rect.h = self.font.size(self.label)[1] + 10
			
		self._has_updated = True
		
	def update(self):
		if self._has_updated:
			
			if self.max_w is None:
				self.image = pygame.Surface( (self.rect.w,self.rect.h) )
			else:
				self.image = pygame.Surface( (self.max_w,self.rect.h) )
				
			self.image = self.image.convert()
			self.image.fill( self.backcolor )			
			
			labelRect = pygame.Rect( (2,2), self.font.size(self.label) )
			labelRect.centery = self.image.get_rect().centery	
			
			if self.max_w is None:
				labelRect.centerx = self.image.get_rect().centerx		
			else:
				labelRect.x = self.max_w - self.font.size(self.label)[0]
		
			labelSurf = self.font.render( self.label, 1, (0,0,0) )	

			self.image.blit( labelSurf,labelRect )	
			
			self._has_updated = False
			


